
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author TQNINH
 */
public class Ex_1 {
    
    public Ex_1() {
        
    }
    
    public static void main(String[] args) {
        byte nMatch = 0;
        Scanner input = new Scanner(System.in);
        input.useDelimiter("\n");
        
        System.out.print("Enter a parent string to find: ");
        String strText = input.next();
        System.out.print("Enter Regular Expression String: ");
        String strSearch = input.next();
        System.out.print("Enter a string to replace: ");
        String strReplace = input.next();
        
        //Step - 1: Create a pattern
        Pattern ptnFind = Pattern.compile(strSearch, Pattern.CASE_INSENSITIVE);
        //Step - 2: Create a matcher
        Matcher mtrSearchResult = ptnFind.matcher(strText);
        
        System.out.println("Result: ");
        //Step - 3: Process the matcher
        while(mtrSearchResult.find()) {
            nMatch++;
            System.out.println(nMatch + ". Start at: " + mtrSearchResult.start()
                    + "\tEnd at: " + mtrSearchResult.end());
        }
        System.out.println("Display: " + nMatch);
        System.out.println("New String: " + mtrSearchResult.replaceAll(strReplace));
    }
}
